package my.fuelwallet.test;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddProfile extends Activity {
	private CarProfileHandle profiles;
	private FuelPriceHandle prices;
	private String fuelName;
	
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.add_profile);
		
		prices = new FuelPriceHandle(this); 
		prices.open();
		
		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		List <String>fuelnameList=prices.getFuelNameList();
		List <String>displayList=new ArrayList<String>();
		for(int i=0;i<fuelnameList.size();i++){
			FuelPrice fuelpriceTemp=prices.getFuelPrice(fuelnameList.get(i));
			displayList.add(fuelpriceTemp.getfuelName());
		}
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    		android.R.layout.simple_spinner_item, displayList);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	    prices.close();
	}
	
	public void onClick(View view) {
    	switch (view.getId()) {
    	case R.id.submit:
    		profiles = new CarProfileHandle(this);
            profiles.open();
            //prices.open();
            EditText vAccountName = (EditText) findViewById(R.id.account_name);
            EditText vDisplacement = (EditText) findViewById(R.id.displacement);
            String accountName = vAccountName.getText().toString();
            String displacement = vDisplacement.getText().toString();
            CarProfile newCarProfile = new CarProfile(accountName, Float.parseFloat(displacement),
            		fuelName);
            profiles.putCarProfile(newCarProfile);
            profiles.close();
            //prices.close();
            
            finish();
            break;
    	}
    }
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent,
	        View view, int pos, long id) {
	    	fuelName = parent.getItemAtPosition(pos).toString();
	      Toast.makeText(parent.getContext(), "The fuel name is " +
	          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}
}